<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>订单变更</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		var subFlag = true;
		$(document).ready(function() {
			
		});
		
		function page(n,s){
			$("#pageNo").val(n);
			$("#pageSize").val(s);
			$("#searchForm").submit();
        	return false;
        }
		
		function shows(n){
			$.ajax({
				 url: "${ctx}/sys/orderschange/form", 
				 data:{"id":n},
				 //async:false,
				 dataType:"json",
				 type:"post",
				 success: function(data){
				   if(data.resultCode == 1){
					   $("#changeTableBody").html(""); 
					   var totalPrice = 0;
					   for(var i=0;i<data.detailList.length;i++){
						   var bean = data.detailList[i];
						   var tr = "<tr>"+
						   "<td>"+bean.goodsCnName+"</td>"+
						   "<td><input id='price"+i+"' type='text' name='newPrice_"+i+"' value='"+bean.price+"' class='required isPrice alertFlag' onchange='priceChange("+i+")' /></td>"+
						   "<td><input id='num"+i+"' type='text' name='newNum_"+i+"' value='"+bean.num+"' class='required digits alertFlag' onchange='priceChange("+i+")' /></td>"+
						   "<td><span id='rowTotalPrice"+i+"'>"+bean.price*bean.num+"</span></td>"+
						   "<input type='hidden' name='id_"+i+"' class='alertFlag' value='"+bean.id+"'/>"+
						   "<input type='hidden' name='oldPrice' value='"+bean.price+"'/>"+
						   "<input type='hidden' name='oldNum' value='"+bean.num+"'/>"+
						   "</tr>";
						   $("#changeTableBody").append(tr);
						   totalPrice += bean.price*bean.num;
					   }
					   var tr = "<tr>"+
					   "<td colspan='4'>运费:";
					   if(data.orders.transportType == 1){
						   tr += "<span id='freight' >"+data.orders.freight+"</span><input type='hidden' id='transportType' value='"+data.orders.transportType+"' />" 
						   totalPrice += parseFloat(data.orders.freight);
					   }else{
						   tr += "到付";
					   }
					   tr +="&nbsp;&nbsp;&nbsp;&nbsp;总计:<span id='totalPrice'>"+totalPrice+"</span>元</td>"+
					   "<input type='hidden' name='ordersId' value='"+data.orders.id+"'/>"+
					   "</tr>";
					   $("#changeTableBody").append(tr); 
					   $("#importBox_goods").show();
				   }else{
					   alert(data.resultMessage);
					   subFlag = true;
				   }
				 },
				 error:function(){
					 alert("网络异常");
				 }
			})
		}
		
		function confirm_imbox(){
			if($("#forms").valid()){
				if(subFlag){
					//subFlag = false;
					//验证通过之后修改name属性方便后台接收为数组
					var alertName = $(".alertFlag");
					for(var i = 0 ; i < alertName.length; i++){
						alertName[i].name = alertName[i].name.substring(0,alertName[i].name.indexOf("_"));
					}
					/*
					$("#id").val(goodsId);
					$("#queryPartsCode2").val($("#queryPartsCode").val());
					$("#queryMaterielCode2").val($("#queryMaterielCode").val());
					$("#queryCnName2").val($("#queryCnName").val());
					$("#querySource2").val($("#querySource").val());
					$("#queryStoreCode2").val($("#queryStoreCode").val());
					*/
					$("#forms").submit();
				}else{
					loading('正在提交，请稍等...');
				}
			}
		}
		
		function close_imbox(){
			$("#importBox_goods").hide();
		}
		
		function priceChange(index){
			if(isNaN(parseInt($("#price"+index).val()))){
				return;
			}
			if(isNaN(parseInt($("#num"+index).val()))){
				return;
			}
			var oldRowTotalPrice = parseFloat($("#rowTotalPrice"+index).html());
			var newRowTotalPrice = parseFloat($("#price"+index).val()) * parseFloat($("#num"+index).val());
			//alert(oldRowTotalPrice);
			//alert(newRowTotalPrice);
			//改变行价格
			$("#rowTotalPrice"+index).html(newRowTotalPrice);
			//改变总价格
			var totalPrice = parseFloat($("#totalPrice").html());
			if($("#transportType").val() == "1"){//如果不是到付需要减去运费
				totalPrice -=  parseFloat($("#freight").html());
			}
			totalPrice -= oldRowTotalPrice;
			totalPrice += newRowTotalPrice;
			//改变运费
			if($("#transportType").val() == "1"){
				$("#freight").html( (totalPrice * 0.1).toFixed(2) );
				$("#totalPrice").html(parseFloat(totalPrice) + parseFloat((totalPrice * 0.1).toFixed(2)));
			}else{
				$("#totalPrice").html(parseFloat(totalPrice));
			}
			
		}

		function remarksChange(obj){
			$("#remarksLength").html($(obj).val().length);
		}
	</script>
	
	<style>
		#importBox_goods{
			position:fixed;
			padding:0 15px;
			width:600px;
			background:#FFF;
			top:10%;
			left:25%;
			margin:0 auto;
			border:1px solid #CCC;
			box-shadow:3px 3px 10px 4px #DDD;
			z-index: 9999;
			
		}
		
		#importBox_goods .ib_bottom{
			margin-bottom:35px;
		}
       
	</style>
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active"><a href="${ctx}/sys/orderschange/">订单变更列表</a></li>
	</ul>
	<form:form id="searchForm" modelAttribute="orders" action="${ctx}/sys/orderschange/" method="post" class="breadcrumb form-search">
		<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
		<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
		<ul class="ul-form">
		
		    <li><label>订单编号：</label>
				<form:input path="code" htmlEscape="false" maxlength="50" class="input-medium"/>
			</li>
			
			<li><label>下单用户类型：</label>
				<form:select id="userType" path="userType" class="input-medium">
			    <form:option value="" label="所有"/>
			    <form:options items="${fns:getDictList('orders_user_type')}" htmlEscape="false" itemValue="value" itemLabel="label"/>
			    </form:select>
			</li>
			 <li><label>下单时间：</label>
				<input name="starPayDate" type="text" readonly="readonly" placeholder="开始时间" class="input-medium Wdate " 
					value="<fmt:formatDate value="${order.createDate }" pattern="yyyy-MM-dd HH:mm:ss"/>"
			         onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
			     &nbsp;&nbsp;&nbsp;&nbsp;
			     <input name="endPayDate" type="text" readonly="readonly" placeholder="结束时间" class="input-medium Wdate " 
					value="<fmt:formatDate  value="${order.createDate }" pattern="yyyy-MM-dd HH:mm:ss"/>"
			     onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/> 
			</li> 
			<li>
			<li><label>下单门店：</label><form:select id="buyStoreCode" path="buyStoreCode" class="input-medium">
			<form:option value="" label="所有"/>
			<form:options items="${store}" htmlEscape="false" itemValue="code" itemLabel="cnName"/>
			</form:select>
			</li>
			<li class="btns"><input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/></li>
			<li class="clearfix"></li>
		</ul>
	</form:form>
	<sys:message content="${message}"/>
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead>
			<tr>
				<th>序号</th>
				<th>订单编号</th>
				<th>下单时间</th>
				<th>用户类型</th>
				<th>下单用户</th>
				<th>订单金额</th>
				<th>下单门店</th>
				<th>状态</th>
				<shiro:hasPermission name="sys:orderschange:edit"><th>操作</th></shiro:hasPermission>
			</tr>
		</thead>
		<tbody>
		<c:forEach items="${page.list}" var="orders" varStatus="status">
			<tr>
			    <td>${status.index+1}</td>
			    <td><a href="${ctx}/sys/orders/form?id=${orders.id}&tagUrl=${ctx}/sys/orderschange/list&tagName=4">${orders.code}</a></td>
				<td>
					<fmt:formatDate value="${orders.createDate}" pattern="yyyy-MM-dd HH:mm:ss"/>
				</td>
				<td>${fns:getDictLabel(orders.userType, 'user_type', '')}</td>
				<td>${orders.userName }</td>
				<td>
				<c:choose>
					<c:when test="${orders.freight !=null && orders.freight!=''}">
						${orders.money + orders.freight}
					</c:when>
					<c:otherwise>
						${orders.money}
					</c:otherwise>
				</c:choose>
				</td>
				<td>${orders.buyStoreName}</td>
				<td>${fns:getDictLabel(orders.state, 'orders_state', '')}</td>
				
				<shiro:hasPermission name="sys:orderschange:edit"><td>
    				<a href="javascript:void(0)" onclick="shows('${orders.id}')">变更</a>
				</td></shiro:hasPermission>
			</tr>
		</c:forEach>
		</tbody>
	</table>
	<div class="pagination">${page}</div>
	
		<!-- 弹框 -->
	<div id="importBox_goods" class="hide">
			 <form action="${ctx}/sys/orderschange/save" method="post" id="forms">
			 	<div style="margin-top:20px;">
					<table id="changeTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th>商品名称</th>
								<th>单价</th>
								<th>购买数量</th>
								<th>小计</th>
							</tr>
						</thead>
						<tbody id="changeTableBody">
							
						</tbody>
					</table>
					变更说明:<textarea rows="4" cols="30" name="remarks" style="resize:vertical;" maxlength="100" onkeyup="remarksChange(this)" ></textarea>
					<span style="vertical-align: bottom;" ><span style="vertical-align: bottom;" id="remarksLength" >0</span>/100</span>
				</div>
			  </form>
			<div class='ib_bottom'>
		 		<input id="btnCancel" class="btn" type="button" style='' value="确定" onclick="confirm_imbox()"/>
				<input id="btnCancel" class="btn" type="button" style='margin-left:10px;' value="取消" onclick="close_imbox()"/>
			</div>	
	</div>
</body>
</html>